package com.caj.service;

import java.util.concurrent.*;

public class CajFutureTask<V> implements Future<V>, Runnable {

    Callable<V> callable;

    V result;

    public CajFutureTask(Callable<V> callable){
        this.callable = callable;
    }
    @Override
    public void run() {
        try {
            result = callable.call();
            synchronized (this){
                notify();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return false;
    }

    @Override
    public V get() throws InterruptedException, ExecutionException {
        if(result != null){
            return result;
        }
        synchronized (this){
            wait();
        }
        return result;
    }

    @Override
    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return null;
    }
}
